

package jetbrains.buildServer.server.rest.data.finder.builder;

import java.util.function.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Maps given single dimension value of the locator to an item.
 *
 * @param <TYPE> type of produced items.
 */
public class DimensionValueMapperImpl<TYPE> implements DimensionValueMapper<TYPE> {
  @NotNull private final Function<String, TYPE> myRetriever;
  @Nullable private String myLocatorTypeDescription;

  public DimensionValueMapperImpl(@NotNull final Function<String, TYPE> retriever) {
    myRetriever = retriever;
  }

  @Override
  @Nullable
  public String getLocatorTypeDescription() {
    return myLocatorTypeDescription;
  }

  @Override
  @Nullable
  public TYPE get(@NotNull final String dimensionValue) {
    return myRetriever.apply(dimensionValue);
  }

  /**
   * Human-readable type description of locator dimension values accepted by this mapper.
   * E.g. number, boolean, etc
   */
  @NotNull
  public DimensionValueMapperImpl<TYPE> acceptingType(@NotNull String locatorDimensionTypeDescription) {
    myLocatorTypeDescription = locatorDimensionTypeDescription;
    return this;
  }
}